#!/bin/bash
# DJVU_Monochrome_PostScript_1.sh ( 13 ) : This script , by Frank Trampe , converts the monochrome layer of each page of a DjVu file to PostScript and then joins the pages .
# This script takes two arguments : the name of the input file and the name of the output file .

[ "x$TMPDIR" == x ] && TMPDIR=/tmp
tmpname="$TMPDIR/Electric_Document_$$.tmp"
mkdir -p "$tmpname"

Page_Count_1=`djvudump $1 | awk '{ count = 0 } /pages/ { for(i=2;i<=NF;i++){ if($i ~ "pages"){b=i-1; print $b}}}'` ;
if [ $4 ] ; then
Start1=$4 ;
else
Start1=1 ;
fi
if [ $5 ] ; then
End1=$5 ;
else
End1=$Page_Count_1 ;
fi 
if [ $2 ] ; then
Copies1=$2 ;
else
Copies1=1 ;
fi
for PNum1 in `seq $Start1 $End1` ; do
ddjvu -mode=black -format=pbm -page=$PNum1 $1 "$tmpname"/Page_`Number_Crop_1 4 $PNum1`.pbm ;
done
cat "$tmpname"/Page_*.pbm | pbmtopsg3 --dpi=600 > $2 ;
rm -r "$tmpname" ;

